_选手1 = ["选手1", 0, 0, "🏓", "#2196F3"]  # 姓名，胜局，当前得分, 状态，颜色
_选手2 = ["选手2", 0, 0, "", "#FF5722"]
_比赛局数: int = 3  # 1,3,5,7代表打几局
_起始发球权: int = 1  # 选手1或选手2开_始发球
_左边 = _选手1
_右边 = _选手2
_当前局: int = 1
_球拍 = "🏓"
_胜利 = "👑"
_比赛进行中 = False


def 比赛进行中() -> bool:
    return _比赛进行中


def 开始比赛(p1, p2, game, start):
    global _选手1, _选手2, _比赛局数, _起始发球权, _左边, _右边, _当前局, _比赛进行中
    _选手1[0:4] = [p1, 0, 0, ""]
    _选手2[0:4] = [p2, 0, 0, ""]
    _比赛局数 = game
    _起始发球权 = start
    _比赛进行中 = True
    # ---------------
    _左边 = _选手1
    _右边 = _选手2
    _当前局 = 1
    _计算发球权()


def 重新开始():
    global _选手1, _选手2, _比赛局数, _起始发球权, _左边, _右边, _当前局, _比赛进行中
    _选手1[1:4] = [0, 0, ""]  # 胜局，当前得分, 状态
    _选手2[1:4] = [0, 0, ""]
    _左边 = _选手1
    _右边 = _选手2
    _当前局 = 1
    _比赛进行中 = False


def 交换场地():
    global _选手1, _选手2, _左边, _右边
    if _左边 == _选手1:
        _左边 = _选手2
        _右边 = _选手1
    else:
        _左边 = _选手1
        _右边 = _选手2


def _当局胜者() -> int:
    s1 = _选手1[2]
    s2 = _选手2[2]
    if s1 >= 11 and s1 - s2 >= 2:
        return 1
    if s2 >= 11 and s2 - s1 >= 2:
        return 2
    return 0


def 下一局():
    global _当前局
    if _当前局 >= _比赛局数:
        return  # 比赛结束

    p = _当局胜者()
    if p == 1:
        if _选手1[1] + 1 > _比赛局数 / 2:
            return  # 比赛结束
        _选手1[1] += 1
    if p == 2:
        if _选手2[1] + 1 > _比赛局数 / 2:
            return  # 比赛结束
        _选手2[1] += 1
    if p == 1 or p == 2:
        _当前局 += 1
        _选手1[2] = 0
        _选手2[2] = 0
        交换场地()
        _计算发球权()


def 左边加分():
    if not (_左边[2] >= 11 and _左边[2] - _右边[2] >= 2):
        _左边[2] += 1
    _计算发球权()


def 左边减分():
    if _左边[2] > 0:
        _左边[2] -= 1
    _计算发球权()


def 右边加分():
    if not (_右边[2] >= 11 and _右边[2] - _左边[2] >= 2):
        _右边[2] += 1
    _计算发球权()


def 右边减分():
    if _右边[2] > 0:
        _右边[2] -= 1
    _计算发球权()


# 发球权只有1和2
def _换发球权(p):
    if p == 1:
        return 2
    return 1


def _计算发球权():
    # 假定选手1先发球, 计算当前比分发球权
    if _选手1[2] <= 10 and _选手2[2] <= 10:
        if (_选手1[2] + _选手2[2]) % 4 in (0, 1):
            p = 1
        else:
            p = 2
    else:  # 10:10后每1分换发球
        if (_选手1[2] + _选手2[2]) % 2 == 0:
            p = 1
        else:
            p = 2

    # 计算当前局起始发球权
    if _当前局 % 2 == 1:
        当前局起始发球权 = _起始发球权
    else:
        当前局起始发球权 = _换发球权(_起始发球权)

    if 当前局起始发球权 == 2:
        # 与预期不符，需要交换发球权
        p = _换发球权(p)

    if p == 1:
        _选手1[3] = _球拍
        _选手2[3] = ""
    else:
        _选手1[3] = ""
        _选手2[3] = _球拍

    p = _当局胜者()
    if p == 1:
        _选手1[3] = _胜利
        _选手2[3] = ""
    if p == 2:
        _选手1[3] = ""
        _选手2[3] = _胜利


def 左边状态():
    return _左边[3]


def 左边姓名():
    return _左边[0]


def 左边胜局():
    return _左边[1]


def 左边得分():
    return _左边[2]


def 右边状态():
    return _右边[3]


def 右边姓名():
    return _右边[0]


def 右边胜局():
    return _右边[1]


def 右边得分():
    return _右边[2]


def 左边颜色():
    return _左边[4]


def 右边颜色():
    return _右边[4]


def 显示比赛局数():
    msg = {
        1: "1局决胜",
        3: "3局2胜",
        5: "5局3胜",
        7: "7局4胜",
    }
    return msg[_比赛局数]


def 比赛局数():
    return _比赛局数


def 当前局():
    return _当前局


def 起始发球权():
    return _起始发球权
